/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import database_beans.Obavestenja;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import server.Log;
import server.Util;

/**
 *
 * @author stefan
 */
@ManagedBean
@RequestScoped
public class TableObavestenja implements Serializable{
    private ArrayList<Obavestenja> obavestenjaList;

    private ArrayList<Obavestenja> filteredList;

    private SelectItem[] filterOptions;

    private ArrayList<Obavestenja> staraObavestenja;
    
    private ArrayList<Obavestenja> novaObavestenja;

    private static String [] kategorije = {"Posao", "Praksa", "Vesti"};
    /**
     * Creates a new instance of TableObavestenjaBean
     */
    public TableObavestenja() {
    }
    
    @PostConstruct
    public void init(){
        Log.i("Konstruktor TableObavestenja");
        filterOptions = createFilterOptions();
        loadObavestenja();
    }

    public ArrayList<Obavestenja> getObavestenjaList() {
        return obavestenjaList;
    }

    public void setObavestenjaList(ArrayList<Obavestenja> obavestenjaList) {
        this.obavestenjaList = obavestenjaList;
    }

    public ArrayList<Obavestenja> getFilteredList() {
        return filteredList;
    }

    public void setFilteredList(ArrayList<Obavestenja> filteredList) {
        this.filteredList = filteredList;
    }

    public SelectItem[] getFilterOptions() {
        return filterOptions;
    }

    public void setFilterOptions(SelectItem[] filterOptions) {
        this.filterOptions = filterOptions;
    }

    public ArrayList<Obavestenja> getStaraObavestenja() {
        return staraObavestenja;
    }

    public void setStaraObavestenja(ArrayList<Obavestenja> staraObavestenja) {
        this.staraObavestenja = staraObavestenja;
    }

    public void loadObavestenja() {

        if (novaObavestenja == null) {
            Session session = Util.getSesion();
            Transaction t = session.beginTransaction();
            try{
            Criteria userCriteria = session.createCriteria(Obavestenja.class);
            long zaArhivu = (long) (System.currentTimeMillis() - 7776000000.0);
            Date date = new Date(zaArhivu);
            userCriteria.add(Restrictions.ge("datum", date));
            obavestenjaList = new ArrayList<Obavestenja>(userCriteria.list());
            t.commit();
            novaObavestenja = new ArrayList<Obavestenja>(obavestenjaList);
            Log.i("U bazi nadjeno " + obavestenjaList.size() + " obavestenja");
            }catch(Exception e){
                e.printStackTrace();
                t.rollback();
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Greska! Neuspesna konekcija sa bazom"));
            }
            obavestenjaList = novaObavestenja;
        } 

    }

    public void loadArhiva() {
        
        if (staraObavestenja == null) {
            Session session = Util.getSesion();
            Transaction t = session.beginTransaction();
            try{
            Criteria userCriteria = session.createCriteria(Obavestenja.class);
            long zaArhivu = (long) (System.currentTimeMillis() - 7776000000.0);
            Date date = new Date(zaArhivu);
            userCriteria.add(Restrictions.lt("datum", date));
            obavestenjaList = new ArrayList<Obavestenja>(userCriteria.list());
            t.commit();
            staraObavestenja = new ArrayList<Obavestenja>(obavestenjaList);
            Log.i("U bazi nadjeno " + obavestenjaList.size() + " arhivirano obavestenja");
            }catch(Exception e){
                t.rollback();
            }
            obavestenjaList = staraObavestenja;
        } 
    }

    private SelectItem[] createFilterOptions() {

        SelectItem[] options = null;

        if (kategorije != null) {
            options = new SelectItem[kategorije.length + 1];
            options[0] = new SelectItem("", "Kategorija");
            int i = 0;
            for (String k : kategorije) {
                options[i + 1] = new SelectItem(k, k);
                i++;
            }

        }
        return options;
    }

   

}
